﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class SalaryGroupDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public SalaryGroupDAL()
        {

        }
        public DataSet LoadSalaryGroup(Nullable<int> Sequence, Nullable<int> EmployeeType, Nullable<int> PositionType, Nullable<int> ClassSeq)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@PositionType", PositionType);
                dbManager.AddParameters(2, "@EmployeeType", EmployeeType);
                dbManager.AddParameters(3, "@ClassSeq", ClassSeq);
                dbManager.AddParameters(4, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = "SELECT s.*"
                        + ", et.EMP_TYPE_NAME, pt.POS_TYPE_NAME, c.CLASS_NAME"
                    + " FROM HRM_PR_SALARY_GROUPS s"
                        + " LEFT JOIN HRM_PS_EMPLOYEE_TYPES et ON s.EMP_TYPE_SEQ = et.EMP_TYPE_SEQ"
                        + " LEFT JOIN HRM_MP_POSITION_TYPES pt ON s.POS_TYPE_SEQ = pt.POS_TYPE_SEQ"
                        + " LEFT JOIN HRM_MP_CLASSES c ON s.CLASS_SEQ = c.CLASS_SEQ"
                    + " WHERE s.RECORD_STATUS = @ActiveRecord"
                    + (Sequence.HasValue ? " AND s.SAL_GRP_SEQ = @Sequence" : "")
                    + (ClassSeq.HasValue ? " AND s.CLASS_SEQ = @ClassSeq" : "")
                    + (PositionType.HasValue ? " AND s.POS_TYPE_SEQ = @PositionType" : "")
                    + (EmployeeType.HasValue ? " AND s.EMP_TYPE_SEQ = @EmployeeType" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);                

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return ds;
        }
        public DataSet LoadSalaryDtls(Nullable<int> Sequence, Nullable<int> ClassSeq)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@ClassSeq", ClassSeq);
                dbManager.AddParameters(2, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = "SELECT d.*"
                        + ", et.EMP_TYPE_NAME, pt.POS_TYPE_NAME, c.CLASS_NAME"
                    + " FROM HRM_PR_SALARY_GROUPS d"
                        + " INNER JOIN HRM_PR_SALARY_GROUPS s ON d.SAL_GRP_SEQ = s.SAL_GRP_SEQ"
                        + " LEFT JOIN HRM_PS_EMPLOYEE_TYPES et ON s.EMP_TYPE_SEQ = et.EMP_TYPE_SEQ"
                        + " LEFT JOIN HRM_MP_POSITION_TYPES pt ON s.POS_TYPE_SEQ = pt.POS_TYPE_SEQ"
                        + " LEFT JOIN HRM_MP_CLASSES c ON s.CLASS_SEQ = c.CLASS_SEQ"
                    + " WHERE d.RECORD_STATUS = @ActiveRecord"
                    + (Sequence.HasValue ? " AND d.GRP_DTLS_SEQ = @Sequence" : "")
                    + (ClassSeq.HasValue ? " AND s.CLASS_SEQ = @ClassSeq" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return ds;
        }
    }
}
